import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';

import '../../resources/kq_theme_colors.dart';
import '../../theme/kq_theme_manager.dart';

// ignore: must_be_immutable
class KqBottomTabBar extends StatefulWidget {
  /// 选中后的颜色,默认：KqThemeManager.getCommonConfig().mainColor
  Color? fixedColor;

  /// 当前选中的 item 索引值，默认：0
  int? currentIndex;

  ///导航项点击回调的点击回调
  ValueChanged<int>? onTap;

  /// 未选中时的 icons iconWidgets和activeIconWidgets必须一起使用
  List<Widget>? iconWidgets;

  /// 选中时的 icon activeIconWidgets和iconWidgets必须一起使用
  List<Widget>? activeIconWidgets;

  /// Tab 标题
  List<Widget>? titleWidgets;

  /// icons icons和activeIcons必须一起使用
  List<String>? icons;

  /// 选中时的 icon activeIcons和icons必须一起使用
  List<String>? activeIcons;

  /// Tab 标题
  List<String> titles;

  /// iconSize
  double? iconSize;

  /// fontSize
  double? fontSize;

  /// bottomNavBar 背景色 默认：白色
  Color? backgroundColor;

  KqBottomTabBar(
      {this.fixedColor = KqThemeColors.textBlue,
        this.currentIndex = 0,
        this.onTap,
        this.iconWidgets,
        this.activeIconWidgets,
        this.titleWidgets,
        this.icons,
        this.activeIcons,
        required this.titles,
        this.iconSize,
        this.fontSize,
        this.backgroundColor,
        super.key})
      : assert((iconWidgets != null &&
      activeIconWidgets != null &&
      titleWidgets != null &&
      iconWidgets.length == activeIconWidgets.length &&
      activeIconWidgets.length == titleWidgets.length) ||
      (icons != null &&
          activeIcons != null &&
          icons.length == activeIcons.length &&
          activeIcons.length == titles.length));

  @override
  State<KqBottomTabBar> createState() => _KqBottomTabBarState();
}

class _KqBottomTabBarState extends State<KqBottomTabBar> {
  List<BottomNavigationBarItem> items = [];

  @override
  void initState() {
    super.initState();
    createItems();
  }

  @override
  void didUpdateWidget(covariant KqBottomTabBar oldWidget) {
    createItems();
    super.didUpdateWidget(oldWidget);
  }

  createItems() {
    items = [];
    if (widget.iconWidgets != null) {
      for (var i = 0; i < widget.iconWidgets!.length; i++) {
        ///tooltip 设置空串禁止长按提示tip
        items.add(BottomNavigationBarItem(
            icon: widget.iconWidgets![i],
            label: widget.titles[i],
            tooltip: '',
            activeIcon: widget.activeIconWidgets![i]));
      }
    } else if (widget.icons != null) {
      for (var i = 0; i < widget.icons!.length; i++) {
        ///tooltip 设置空串禁止长按提示tip
        items.add(BottomNavigationBarItem(
            icon: Image.asset(
              widget.icons![i],
              width: widget.iconSize ?? 28.r,
              height: widget.iconSize ?? 28.r,
              fit: BoxFit.fill,
            ),
            activeIcon: Image.asset(
              widget.activeIcons![i],
              width: widget.iconSize ?? 28.r,
              height: widget.iconSize ?? 28.r,
              fit: BoxFit.fill,
            ),
            tooltip: '',
            label: widget.titles[i]));
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return BottomNavigationBar(
      items: items,
      currentIndex: widget.currentIndex ?? 0,
      onTap: widget.onTap,
      elevation: 0,
      type: BottomNavigationBarType.fixed,
      fixedColor:
      widget.fixedColor ?? KqThemeManager.getCommonConfig().mainColor,
      selectedFontSize: widget.fontSize ?? 14.sp,
      unselectedFontSize: widget.fontSize ?? 14.sp,
      backgroundColor: widget.backgroundColor ?? KqThemeColors.bgWhite,
    );
  }
}
